#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash motioncor2-1.5.0.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* ||  "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* ||  "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
set -e

cp dependency/src/MotionCor2_1.5.0.zip  $buildpath
cd $buildpath
unzip MotionCor2_1.5.0.zip  && rm -rf MotionCor2_1.5.0.zip
cd -
cp -r $buildpath/MotionCor2_1.5.0 $installpath/

cat>"$installpath/MotionCor2_1.5.0/motioncor2_modulefiles"<<EOF
#%Module1.0
conflict ctffind
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set motioncor2 \$pwd
prepend-path PATH \$motioncor2
EOF

rm -rf $buildpath/MotionCor2_1.5.0 
rm -rf $buildpath/__MACOSX
